/*
 * Copyright (c) @CompanyNameMagicTag 2018-2022. All rights reserved.
 * Description:  Hi11xx SEC MOUDLE CONFIG
 * Author: @CompanyNameTag
 * Create: 2021-05-26
 */
#ifndef PAL_SEC_CONFIG_PORT_H
#define PAL_SEC_CONFIG_PORT_H

#include "chip_definitions.h"
#include "arch_port.h"
#include "product.h"
#include "memory_config.h"
#include "ssb_config_common.h"
#include "platform_core.h"
#include "otp_map.h"
#include "interrupt.h"
#if defined(USE_CMSIS_OS) && defined(__LITEOS__)
#include "los_hwi.h"
#endif

#if defined(BUILD_APPLICATION_SSB) && MASTER_ONLY
#include "ssb_fota.h"
#endif

/****************** INTRUPT CONFIG **********************/
#if CORE == BT
#define SEC_INT_NUMBER  TRNG_INT_IRQN
#else
#define SEC_INT_NUMBER  SEC_INT_IRQN
#endif
#define SEC_INT_PRI     irq_prio(SEC_INT_NUMBER)

#if CHIP_LIBRA
#define PAL_IMAGE_SIGN_MAGIC_CODE 0x53426363 // 'SBcc'
#else
#define PAL_IMAGE_SIGN_MAGIC_CODE 0x31313378 // '113x'
#endif

/****************** REG CONFIG **************************/
#define PAL_SEC_CTRL_REG_BASE   SEC_CTL_RB_BASE
#define PAL_RSAV2_REG_BASE      RSAV2_S_RB_BASE
#define PAL_TRNG_REG_BASE       TRNG_RB_BASE
#define PAL_M_CTRL_REG_BASE     M_CTL_RB_BASE

#if CORE == SECURITY
#define PAL_SEC_SUB_REG_BASE    SEC_SUB_CTL_RB_BASE
#endif

/****************** OTP CONFIG **************************/
#define PAL_OTP_BOOTLOADER_BYTE                     OTP_BOOTLOADER_BYTE
#define PAL_OTP_BOOTLOADER_FSB_DIS_PKCS1_ALG_BIT    OTP_BOOTLOADER_FSB_DIS_PKCS1_ALG_BIT

#if CORE == SECURITY
// SEC SSB Image Data
#define PAL_OTP_SSB_CODE_SIZE_START           OTP_SEC_SSB_CODE_SIZE_START
#define PAL_OTP_SSB_CODE_SIZE_LENGTH          OTP_SEC_SSB_CODE_SIZE_LENGTH
#define PAL_OTP_SSB_CODE_CRC                  OTP_SEC_SSB_CODE_CRC

#define PAL_OTP_SSB_SHA256_START              OTP_SEC_SSB_SHA256_START
#define PAL_OTP_SSB_SHA256_LENGTH             OTP_SEC_SSB_SHA256_LENGTH
#define PAL_OTP_SSB_SHA256_CRC                OTP_SEC_SSB_SHA256_CRC
#else
#define PAL_OTP_SSB_CODE_SIZE_START           OTP_SSB_CODE_SIZE_START
#define PAL_OTP_SSB_CODE_SIZE_LENGTH          OTP_SSB_CODE_SIZE_LENGTH
#define PAL_OTP_SSB_CODE_CRC                  OTP_SSB_CODE_CRC

#define PAL_OTP_SSB_SHA256_START              OTP_SSB_SHA256_START
#define PAL_OTP_SSB_SHA256_LENGTH             OTP_SSB_SHA256_LENGTH
#define PAL_OTP_SSB_SHA256_CRC                OTP_SSB_SHA256_CRC
#endif

// McuRootPubKey hash
#define PAL_OTP_MCU_ROOT_PUBKEY_SHA256_START  OTP_MCU_ROOT_PUBKEY_SHA256_START
#define PAL_OTP_MCU_ROOT_PUBKEY_SHA256_LENGTH OTP_MCU_ROOT_PUBKEY_SHA256_LENGTH
#define PAL_OTP_MCU_ROOT_PUBKEY_SHA256_CRC    OTP_MCU_ROOT_PUBKEY_SHA256_CRC

// McuSubkeyIdMask
#define PAL_OTP_MCU_SUBKEY_ID_MASK_START      OTP_MCU_SUBKEY_ID_MASK_START
#define PAL_OTP_MCU_SUBKEY_ID_MASK_LENGTH     OTP_MCU_SUBKEY_ID_MASK_LENGTH
#define PAL_OTP_MCU_SUBKEY_ID_MASK_CRC        OTP_MCU_SUBKEY_ID_MASK_CRC

// McuSubkeyCategory
#define PAL_OTP_MCU_SUBKEY_CATEGORY_START     OTP_MCU_SUBKEY_CATEGORY_START
#define PAL_OTP_MCU_SUBKEY_CATEGORY_LENGTH    OTP_MCU_SUBKEY_CATEGORY_LENGTH
#define PAL_OTP_MCU_SUBKEY_CATEGORY_CRC       OTP_MCU_SUBKEY_CATEGORY_CRC

// SEC_BOOT_DEBUG
#define PAL_OTP_SEC_BOOT_DEBUG_FLAG           OTP_SEC_BOOT_DEBUG_FLAG
#define PAL_OTP_SEC_BOOT_DEBUG_FLAG_LENGTH    OTP_SEC_BOOT_DEBUG_FLAG_LENGTH
#define PAL_OTP_SEC_BOOT_EN_BIT               OTP_SEC_BOOT_EN_BIT
#define PAL_OTP_SEC_BOOT_DEBUG_BIT            OTP_SEC_BOOT_DEBUG_BIT
#define PAL_OTP_SEC_BOOT_KEY_CHECK_EN_BIT     OTP_SEC_BOOT_KEY_CHECK_EN_BIT
#if (SEC_IAMGE_AES_DECRYPT_EN == YES)
#define PAL_OTP_SEC_BOOT_AES_EN_BIT           OTP_SEC_BOOT_AES_EN_BIT
#endif

// SSB VERSION COUNTER
#define PAL_OTP_SSB_VERSION_COUNTER_START     OTP_SSB_VERSION_COUNTER_START
#define PAL_OTP_SSB_VERSION_LENGTH            OTP_SSB_VERSION_LENGTH
#define PAL_OTP_SSB_VERSION_LENGTH_BIT        OTP_SSB_VERSION_LENGTH_BIT

// FSB Bootloader Double Check same as OTP_BOOTLOADER_BYTE
#define PAL_OTP_BOOTLOADER_DOUBLE_BYTE        OTP_BOOTLOADER_DOUBLE_BYTE

// FSB Bootloader Double Check same as OTP_SEC_BOOT_DEBUG_FLAG
#define PAL_OTP_SEC_BOOT_DEBUG_DOUBLE_FLAG    OTP_SEC_BOOT_DEBUG_DOUBLE_FLAG

/****************** MEMORY CONFIG **************************/
#define PAL_SSB_BOOT_RAM_ORIGIN             SSB_BOOT_RAM_ORIGIN

// flash
#define PAL_FLASH_START                     FLASH_START
#define PAL_FLASH_MAX_END                   FLASH_MAX_END
#define PAL_FLASH_PAGE_SIZE                 FLASH_PAGE_SIZE

// rom used for ssb image signature vertify
#define PAL_MCU_ROOT_PUBKEY_START           MCU_ROOT_PUBKEY_START
#define PAL_MCU_SUB_PUBKEY_START            MCU_SUB_PUBKEY_START
#define PAL_MCU_SSB_IMAGE_SIGN_START        MCU_SSB_IMAGE_SIGN_START
#define PAL_MCU_SUB_PUBKEY_BACKUP_START     MCU_SUB_PUBKEY_BACKUP_START
#define PAL_MCU_SSB_IMAGE_SIGN_BACKUP_START MCU_SSB_IMAGE_SIGN_BACKUP_START

#define PAL_MCU_SEC_BOOT_SIGN_BACKUP_START  MCU_SEC_BOOT_SIGN_BACKUP_START
#define PAL_MCU_SEC_BOOT_SIGN_BACKUP_LEN    MCU_SEC_BOOT_SIGN_BACKUP_LEN

// ssb used for image signature vertify
#if defined(BUILD_APPLICATION_SSB) && MASTER_ONLY
#define PAL_MCU_RECOVERY_IMAGE_SIGN_START   MCU_RECOVERY_IMAGE_SIGN_START
#define PAL_MCU_BT_IMAGE_SIGN_START         MCU_BT_IMAGE_SIGN_START
#if DSP_EXIST == YES
#define PAL_MCU_DSP_IMAGE_SIGN_START        MCU_DSP_IMAGE_SIGN_START
#endif
#define PAL_MCU_APP_IMAGE_SIGN_START        MCU_APP_IMAGE_SIGN_START

#if DUAL_DSP_EXIST == YES
#define PAL_MCU_DSP1_IMAGE_SIGN_START       MCU_DSP1_IMAGE_SIGN_START
#endif

#define PAL_SSB_FLASH_REGION_START          SSB_FLASH_REGION_START
#define PAL_SSB_BACKUP_FLASH_REGION_START   SSB_BACKUP_FLASH_REGION_START
#define PAL_SSB_BACKUP_FLASH_REGION_LENGTH  SSB_BACKUP_FLASH_REGION_LENGTH

// ssb fota used
#define PAL_FOTA_ROOT_PUBKEY_START          FOTA_ROOT_PUBKEY_START
#define PAL_FOTA_SUB_PUBKEY_START           FOTA_SUB_PUBKEY_START
#define PAL_FOTA_SSB_SIGN_START             FOTA_SSB_SIGN_START
#define PAL_FOTA_RECOVERY_SIGN_START        FOTA_RECOVERY_SIGN_START
#define PAL_FOTA_BT_SIGN_START              FOTA_BT_SIGN_START
#define PAL_FOTA_DSP_SIGN_START             FOTA_DSP_SIGN_START
#define PAL_FOTA_DSP1_SIGN_START            FOTA_DSP1_SIGN_START
#define PAL_FOTA_APP_SIGN_START             FOTA_APP_SIGN_START

#define PAL_FOTA_BIN_MAX_INDEX              FOTA_BIN_MAX_INDEX
typedef fota_bin_index_t pal_fota_bin_index_t;
#endif
#endif